package com.ruoyi.common.utils.bean;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import org.joda.time.LocalDateTime;
import org.springframework.cglib.beans.BeanMap;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MapToEntity {
    public static void main(String[] args) throws IllegalAccessException, ClassNotFoundException {
//        // 创建一个包含键值对的Map
        Map<String, Object> map = new HashMap<>();
        map.put("name", "John");
        map.put("age", 25);
//        map.put("createTime", System.currentTimeMillis());
//        map.put("createTime", "2023-05-05 12:00:00");
//        String s = JSON.toJSONString(map);
//        Person person = JSON.parseObject(s, Person.class);
//        System.out.println(person);
        // 调用方法将Map转换为实体类
        Person person = convertMapToObject(Person.class, map);

        System.out.println(person.getName());   // 输出："John"
        System.out.println(person.getAge());     // 输出：25

//        new MapToEntity().mapToObj1();
//        new MapToEntity().mapToObj2();
    }

    public static Map<String, Object> initMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", 1L);
        map.put("userName", "张三");
        map.put("age", 18);
        map.put("weight", 58.6);
        return map;
    }

    public void mapToObj1() {
        Map<String, Object> map = initMap();
        User user = new User();
        BeanMap beanMap = BeanMap.create(user);
        beanMap.putAll(map);
        System.out.println(user);
    }

    public void mapToObj2() {
        Map<String, Object> map = initMap();
        String s = JSON.toJSONString(map);
        User user = JSON.parseObject(s, User.class);
        System.out.println(user);
    }

    public static User initUser() {
        User user = new User();
        user.setUserId(2L);
        user.setUserName("李四");
        user.setAge(19);
        user.setWeight(60.4);
        return user;
    }

    public void objToMap1() {
        User user = initUser();
        BeanMap beanMap = BeanMap.create(user);
        Map<String, Object> map = beanMap;
        System.out.println(map);
    }

    public void objToMap2() {
        User user = initUser();
        String s = JSON.toJSONString(user);
        Map<String, Object> map = JSON.parseObject(s, new TypeReference<Map<String, Object>>() {
        });
        //下面这种方式也可以转
        //Map<String, Object> map = JSON.parseObject(s, HashMap.class);
        System.out.println(map);
    }

    private static <T> T convertMapToObject(Class<? extends T> clazz, Map<String, Object> map) throws IllegalAccessException, ClassNotFoundException {
        T obj = null;
        try {
            obj = (T) Class.forName(clazz.getName()).newInstance();

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (null != entry.getValue()) {
                    System.out.println(entry.getValue());
                    String fieldName = entry.getKey();

                    Field field = clazz.getDeclaredField(fieldName);
                    if (!field.isAccessible()) {
                        field.setAccessible(true);
                    }
                    if (field.getType() == java.util.Date.class) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = sdf.parse(entry.getValue().toString());
                        field.set(obj, date);
                    } else {
                        field.set(obj, entry.getValue());
                    }
                    field.set(obj, entry.getValue());
                }
            }
        } catch (InstantiationException | NoSuchFieldException | ParseException e) {
            e.printStackTrace();
        }

        return obj;
    }
}
//    private static <T> T convertMapToObject(Class<? extends T> clazz, Map<String, Object> map) throws Exception {
//        T obj = clazz.newInstance();
//        BeanUtils.populate(obj, map);
//
//        return obj;
//    }




